package cn.flashapp.queue.reponsitory;

import com.koolearn.util.SystemGlobals;

public class NotificationQueue extends QueueReponsitory {
	private NotificationQueue() {

	}
	
	@Override
	public String getClientName() {
		return getCname();
	}
	
	private String cname;
	
	public String getCname() {
		return cname;
	}

	public void setCname(String cname) {
		this.cname = cname;
	}

	private static NotificationQueue instance;

	private static String top_level_queue_key = SystemGlobals.getPreference("top_level_notification_queue_key");
	private static String other_level_queue_key = SystemGlobals.getPreference("other_level_notification_queue_key");

	public static NotificationQueue getInstance() {
		if (instance == null) {
			instance = new NotificationQueue();
		}
		return instance;
	}

	/**
	 * 将Top消息放入队列.
	 */
	public void pushTopMsgToQueue( String msg ) throws Exception {
		if (msg != null) {
			byte[] bytes = new String(msg).getBytes("UTF-8");
			this.pushCacheToQueueFloor(top_level_queue_key, bytes );
		}
	}
	
	/**
	 * 从队列中取出Top消息
	 */
	public String popTopMsgFromQueue() throws Exception {
		byte[] bytes = (byte[])this.popCacheFromQueueTop(top_level_queue_key, byte[].class);
		return new String(bytes, "UTF-8");
	}
	
	/**
	 * 将Other消息放入队列.
	 */
	public void pushOtherMsgToQueue( String msg ) throws Exception {
		if (msg != null) {
			byte[] bytes = new String(msg).getBytes("UTF-8");
			this.pushCacheToQueueFloor(other_level_queue_key, bytes );
		}
	}
	/**
	 * 从队列中取出Other消息
	 */
	public String popOtherMsgFromQueue() throws Exception {
		byte[] bytes = (byte[])this.popCacheFromQueueTop(other_level_queue_key, byte[].class);
		return new String(bytes, "UTF-8");
	}

}
